const path = require('path')
const webpack = require('webpack')
const merge = require('webpack-merge')
const TARGET = process.env.npm_lifecycle_event

process.env.BABEL_ENV = TARGET

const PATHS = {
  src: path.join(__dirname, 'src'),
}

const common = {
  entry: [
    'babel-polyfill',
    './src/app',
  ],
  resolve: {
    extensions: ['', '.js', '.jsx', '.less']
  },
  output: {
    path: PATHS.src,
    filename: 'bundle.js',
  },
  module: {
    loaders: [
      {
      test: /\.jsx?$/,
        loaders: ['babel?cacheDirectory'],
        include: PATHS.src,
      }
    ]
  },
}

// Default configuration
if(TARGET === 'start' || !TARGET) {
  module.exports = merge(common, {
    devtool: 'eval-source-map',
    devServer: {
      contentBase: PATHS.src,
      historyApiFallback: true,
      hot: true,
      inline: true,
      progress: true,
      // Display only errors to reduce the amount of output.
      stats: 'errors-only',
      host: '0.0.0.0',
      port: 9001,
    },
    plugins: [
      new webpack.HotModuleReplacementPlugin(),
    ]
  })
}

if(TARGET === 'build') {
  module.exports = merge(common, {})
}